import 'dart:async';

import 'package:dio/dio.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:xiaomi_shop/app/api/api.dart';
import 'package:xiaomi_shop/app/services/xhttp.dart';
import 'package:xiaomi_shop/models/index.dart';

class HomeController extends GetxController {
  // 控制导航栏是否透明
  Rx<bool> isTop = Rx<bool>(true);
  final ScrollController scrollController = ScrollController();
  Rx<List<FocusItemModel>> swiperList = Rx<List<FocusItemModel>>([]);
  var shopCategoryList = <ShopCategoryModel>[].obs;
  var hotShopBannerList = <FocusItemModel>[].obs;
  var hotShopList = <ShopModel>[].obs;
  var bestShopList = <ShopModel>[].obs;

  @override
  void onInit() {
    super.onInit();
    // 监听ListView 滑动的距离
    scrollControllerListener();
    // 请求数据
    getFocusData();
    // 请求商品分类
    getShopCategory();
    // 请求甄选轮播图
    getHotShopBannerData();
    // 请求热销商品
    getHotShopListData();
    // 优惠商品推荐
    getBestShopListData();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }

  /// 监听ListView 滑动的距离
  void scrollControllerListener() {
    scrollController.addListener(() {
      if (scrollController.position.pixels < 10) {
        isTop.value = true;
      } else {
        isTop.value = false;
      }
    });
  }

  /// 请求数据
  void getFocusData() {
    XHttp.getInstance().get(Api.focus).then((value) {
      Result result = value as Result;
      List list = result.data["result"] as List;
      swiperList.value = list
          .map((e) => FocusItemModel.fromJson(e as Map<String, dynamic>))
          .toList();
    });
  }

  // 请求商品分类
  void getShopCategory() {
    XHttp.getInstance().get(Api.shopCategory).then((value) {
      Result result = value as Result;
      List list = result.data["result"] as List;
      shopCategoryList.value = list
          .map((e) => ShopCategoryModel.fromJson(e as Map<String, dynamic>))
          .toList();
      shopCategoryList.removeLast();
      print(shopCategoryList.length);
    });
  }

  // 请求甄选轮播图
  void getHotShopBannerData() {
    XHttp.getInstance().get(Api.focus, {"position": 2}).then((value) {
      Result result = value as Result;
      List list = result.data["result"] as List;
      hotShopBannerList.value = list
          .map((e) => FocusItemModel.fromJson(e as Map<String, dynamic>))
          .toList();
    });
  }

  // 请求热销商品
  void getHotShopListData() {
    XHttp.getInstance()
        .get(Api.shopList, {"is_hot": 1, "page_size": 3}).then((value) {
      Result result = value as Result;
      List list = result.data["result"] as List;
      hotShopList.value = list
          .map((e) => ShopModel.fromJson(e as Map<String, dynamic>))
          .toList();
    });
  }

  // 优惠商品推荐
  void getBestShopListData() {
    XHttp.getInstance().get(Api.shopList, {"is_best": 1}).then((value) {
      Result result = value as Result;
      List list = result.data["result"] as List;
      bestShopList.value = list
          .map((e) => ShopModel.fromJson(e as Map<String, dynamic>))
          .toList();
    });
  }
}
